/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.LoaiPhongControl;
import edu.nii.qlks.DataTransferObject.LoaiPhongInfo;
import java.awt.Color;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author KUUT
 */
public class JInternal_ThemLoaiPhong extends javax.swing.JInternalFrame {

    LoaiPhongControl loaiphong_control;
    LoaiPhongInfo loaiphong_info;
    Vector table_record;
    Vector table_title;
    private static int row;
    static int kiemtra_ThemHuy = 1;
    static int them = 1;
    static int huy = 2;

    public JInternal_ThemLoaiPhong() throws Exception{
        initComponents();
        float[] color = new float[3];
        color = Color.RGBtoHSB(255,255,255,color);
        this.getContentPane().setBackground(Color.getHSBColor(color[0], color[1], color[2]));
        
        loaiphong_control = new LoaiPhongControl();
        loaiphong_info = new LoaiPhongInfo();
        
        table_record = new Vector();
        table_title = new Vector();
        
        table_title.add("STT");
        table_title.add("Tên loại phòng");
        table_title.add("Mã Loại phòng");
        table_title.add("Đơn giá");
        
        LayDS_LoaiPhong();
    }
    
    public void LayDS_LoaiPhong()throws Exception{
        table_record = loaiphong_control.LayDS_LoaiPhong();
        jXTable_DSLoaiphong.setModel(new DefaultTableModel(table_record, table_title));
    }
    
    public void MaLoaiPhong_TuDong(){
        String[] a = null;
        try{
            a = loaiphong_control.LayMaLoaiPhong();
        }catch(Exception e){
            
        }
        
        loaiphong_info.setMaLoaiPhong(MaLoaiPhong_TuDong(a[0]));
        jXTextField_Maloaiphong.setText(jXTextField_Tenloaiphong.getText().trim() + 
                loaiphong_info.getMaLoaiPhong().trim());
    }
    
    public String MaLoaiPhong_TuDong(String ss){
        
        String so2;
        so2 = ss.substring(ss.length() - 3, ss.length());
        int b = Integer.parseInt(so2);
        b = b + 1;
        String s = "" + b;
        
        switch(s.length()){
            case 1:
                s = "00".concat(s);
                break;
            case 2:
                s = "0".concat(s);
                break;
        }
        
        return s;
    }
    
    public boolean trungTenPhong(String s){
        for (int i = 0; i < jXTable_DSLoaiphong.getRowCount(); i++) {
            if(s.equals(jXTable_DSLoaiphong.getValueAt(i, 1).toString().trim()))
                return true;
        }
        return false;
    }
    
    public void themAction(){
        String[] a = null;
        if(jXTextField_Tenloaiphong.getText().equals("") || jXTextField_Dongia.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Tên loại phòng và Đơn giá phòng không được để trống"
                        , "Warning", JOptionPane.WARNING_MESSAGE);
                return;
        }
        if(trungTenPhong(jXTextField_Tenloaiphong.getText())){
                    JOptionPane.showMessageDialog(null, "Trùng với tên phòng khác, Vui lòng chọn tên khác"
                            , "Thông tin", JOptionPane.INFORMATION_MESSAGE);
                    return;
        }
        try{
            a = loaiphong_control.LayMaLoaiPhong();
            loaiphong_info.setMaLoaiPhong(MaLoaiPhong_TuDong(a[0]).trim());
            loaiphong_info.setTenLoaiPhong(jXTextField_Tenloaiphong.getText().trim());
            loaiphong_info.setDonGiaPhong(Integer.parseInt(jXTextField_Dongia.getText().trim()));
            loaiphong_info.setMaLoaiPhong(jXTextField_Tenloaiphong.getText().trim() + MaLoaiPhong_TuDong(a[0]).trim());

            loaiphong_control.Insert(loaiphong_info);
            LayDS_LoaiPhong();
            clear();
        }catch(Exception e){
            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jXTextField_Tenloaiphong = new org.jdesktop.swingx.JXTextField();
        jXTextField_Dongia = new org.jdesktop.swingx.JXTextField();
        jXLabel4 = new org.jdesktop.swingx.JXLabel();
        jXTextField_Maloaiphong = new org.jdesktop.swingx.JXTextField();
        jXButton_Them = new org.jdesktop.swingx.JXButton();
        jXButton_Thoat = new org.jdesktop.swingx.JXButton();
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable_DSLoaiphong = new org.jdesktop.swingx.JXTable();
        jXButton_Them1 = new org.jdesktop.swingx.JXButton();
        jXButton_Them2 = new org.jdesktop.swingx.JXButton();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jXPanel1.setBackground(new java.awt.Color(0, 51, 204));

        jXLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jXLabel1.setText("THÊM LOẠI PHÒNG");
        jXLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(372, 372, 372)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                .addContainerGap())
        );

        jXPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin loại phòng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 18), new java.awt.Color(255, 0, 0))); // NOI18N

        jXLabel2.setText("Tên loại phòng");

        jXLabel3.setText("Đơn giá");

        jXTextField_Tenloaiphong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXTextField_TenloaiphongActionPerformed(evt);
            }
        });
        jXTextField_Tenloaiphong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jXTextField_TenloaiphongKeyReleased(evt);
            }
        });

        jXTextField_Dongia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXTextField_DongiaActionPerformed(evt);
            }
        });
        jXTextField_Dongia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jXTextField_DongiaKeyReleased(evt);
            }
        });

        jXLabel4.setText("Mã loại phòng");

        jXTextField_Maloaiphong.setDisabledTextColor(new java.awt.Color(255, 0, 0));
        jXTextField_Maloaiphong.setEnabled(false);

        jXButton_Them.setBackground(new java.awt.Color(0, 51, 204));
        jXButton_Them.setForeground(new java.awt.Color(255, 255, 255));
        jXButton_Them.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icon/add.png"))); // NOI18N
        jXButton_Them.setText("THÊM");
        jXButton_Them.setBorderPainted(false);
        jXButton_Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_ThemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel2Layout = new javax.swing.GroupLayout(jXPanel2);
        jXPanel2.setLayout(jXPanel2Layout);
        jXPanel2Layout.setHorizontalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel2Layout.createSequentialGroup()
                        .addComponent(jXTextField_Dongia, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel2Layout.createSequentialGroup()
                        .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jXPanel2Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jXButton_Them, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jXPanel2Layout.createSequentialGroup()
                                .addComponent(jXTextField_Tenloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                                .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32)
                                .addComponent(jXTextField_Maloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(101, 101, 101))))
        );
        jXPanel2Layout.setVerticalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXTextField_Tenloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXTextField_Maloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXTextField_Dongia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(jXButton_Them, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jXButton_Thoat.setBackground(new java.awt.Color(0, 51, 204));
        jXButton_Thoat.setForeground(new java.awt.Color(255, 255, 255));
        jXButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icon/exit.png"))); // NOI18N
        jXButton_Thoat.setText("THOÁT");
        jXButton_Thoat.setBorderPainted(false);
        jXButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_ThoatActionPerformed(evt);
            }
        });

        jXPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách loại phòng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 18), new java.awt.Color(255, 0, 0))); // NOI18N

        jXTable_DSLoaiphong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT" ,"Tên phòng", "Mã loại phòng", "Đơn giá phòng"
            }
        ));
        jXTable_DSLoaiphong.setEditable(false);
        jXTable_DSLoaiphong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jXTable_DSLoaiphongMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jXTable_DSLoaiphong);

        javax.swing.GroupLayout jXPanel3Layout = new javax.swing.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel3Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 888, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
        );

        jXButton_Them1.setBackground(new java.awt.Color(0, 51, 204));
        jXButton_Them1.setForeground(new java.awt.Color(255, 255, 255));
        jXButton_Them1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icon/remove.png"))); // NOI18N
        jXButton_Them1.setText("XOÁ");
        jXButton_Them1.setBorderPainted(false);
        jXButton_Them1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_Them1ActionPerformed(evt);
            }
        });

        jXButton_Them2.setBackground(new java.awt.Color(0, 51, 204));
        jXButton_Them2.setForeground(new java.awt.Color(255, 255, 255));
        jXButton_Them2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icon/edit.png"))); // NOI18N
        jXButton_Them2.setText("SỬA");
        jXButton_Them2.setBorderPainted(false);
        jXButton_Them2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_Them2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(47, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jXPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jXButton_Them2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jXButton_Them1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jXButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jXPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(66, 66, 66))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(jXPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(jXPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXButton_Them1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXButton_Them2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jXTextField_TenloaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXTextField_TenloaiphongActionPerformed
        themAction();
    }//GEN-LAST:event_jXTextField_TenloaiphongActionPerformed

    private void jXTextField_TenloaiphongKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jXTextField_TenloaiphongKeyReleased

        if(jXTextField_Tenloaiphong.getText().length() > 10){
            JOptionPane.showMessageDialog(null, "Độ dài Tên loại phòng vượt quá quy định của khách sạn"
                , "Thông tin", JOptionPane.INFORMATION_MESSAGE);
            jXTable_DSLoaiphong.setSelectionMode(0);
            clear();
            return;
        }
        if(kiemtra_ThemHuy != huy){
            MaLoaiPhong_TuDong();
        }

        try{
            if(CheckSpecial(jXTextField_Tenloaiphong.getText())){
                JOptionPane.showMessageDialog(null, "Tên loại phòng không được chứa kí tự đặc biệt"
                    , "Thông tin", JOptionPane.INFORMATION_MESSAGE);
                jXTextField_Tenloaiphong.setText(null);
            }
        }catch(Exception e){}

    }//GEN-LAST:event_jXTextField_TenloaiphongKeyReleased

    private void jXTextField_DongiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXTextField_DongiaActionPerformed
        themAction();
    }//GEN-LAST:event_jXTextField_DongiaActionPerformed

    private void jXTextField_DongiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jXTextField_DongiaKeyReleased
        if(jXTextField_Dongia.getText().length() > 10){
            JOptionPane.showMessageDialog(null, "Đơn giá lớn quá quy định của khách sạn"
                , "Warning", JOptionPane.WARNING_MESSAGE);
            jXTable_DSLoaiphong.setSelectionMode(0);
            clear();
            return;
        }

        try{
            if(CheckNumBer(jXTextField_Dongia.getText())){
                JOptionPane.showMessageDialog(null, "Đơn giá không được nhập chữ hoặc số thập phân"
                    , "Warning", JOptionPane.WARNING_MESSAGE);
                jXTextField_Dongia.setText(null);
                return;
            }
        }catch(Exception e){}
    }//GEN-LAST:event_jXTextField_DongiaKeyReleased

    private void jXButton_ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_ThemActionPerformed

        if(kiemtra_ThemHuy == them){
            themAction();
            clear();
        }
        else if(kiemtra_ThemHuy == huy){
            them();
            jXTable_DSLoaiphong.setSelectionMode(0);
            clear();
        }
    }//GEN-LAST:event_jXButton_ThemActionPerformed

     public boolean CheckSpecial(String s) throws Exception{
        Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(s);
        boolean b = m.find();

        if (b){
           return true;
        }
        else{ 
            return false;
        }
    }
    
    public boolean CheckNumBer(String s) throws Exception{
        Pattern p = Pattern.compile("[^0-9 ]", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(s);
        boolean b = m.find();

        if (b){
           return true;
        }
        else{ 
            return false;
        }
    }
            
    public boolean CheckNumberER(String s) throws Exception{
        try
        {
            String regex = "(^-)*\\d+";

            if(s.matches(regex))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
    
    public void clear(){
        jXTextField_Dongia.setText(null);
        jXTextField_Tenloaiphong.setText(null);
        jXTextField_Maloaiphong.setText(null);
    }
    
    public void huy(){
        jXButton_Them.setIcon(new ImageIcon(getClass().getResource("/icon/icon/remove.png"))); // NOI18N
        jXButton_Them.setText("HUỶ");
        kiemtra_ThemHuy = huy;
    }
    
    public void them(){
        jXButton_Them.setIcon(new ImageIcon(getClass().getResource("/icon/icon/add.png"))); // NOI18N
        jXButton_Them.setText("THÊM");
        kiemtra_ThemHuy = them;
    }
    
    private void jXButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_ThoatActionPerformed
        this.setVisible(false);
        this.removeNotify();
    }//GEN-LAST:event_jXButton_ThoatActionPerformed

    private void jXTable_DSLoaiphongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXTable_DSLoaiphongMouseClicked
        //Kiểm tra đã click vào Bảng danh sách phòng hay không

        if(jXTable_DSLoaiphong.getSelectedRow() < 0){
            JOptionPane.showMessageDialog(null, "Vui lòng kích các dòng trong bảng Danh sách Loại phòng"
                , "Thông tin", JOptionPane.INFORMATION_MESSAGE);

            return;
        }

        try{
            //Chọn row trong table
            row = jXTable_DSLoaiphong.getSelectedRow();

            //Gán giá trị từ bảng cho các đối tượng
            jXTextField_Tenloaiphong.setText(jXTable_DSLoaiphong.getValueAt(row, 1).toString().trim());
            jXTextField_Maloaiphong.setText(jXTable_DSLoaiphong.getValueAt(row, 2).toString().trim());
            float a = Float.parseFloat(jXTable_DSLoaiphong.getValueAt(row, 3).toString().trim());
            String s = String.valueOf((int)a);
            jXTextField_Dongia.setText(s);
            huy();
        }catch(Exception e){

        }

    }//GEN-LAST:event_jXTable_DSLoaiphongMouseClicked

    private void jXButton_Them1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_Them1ActionPerformed
        Object option[] = {"Có", "Không"};
        if(JOptionPane.showOptionDialog(null, " Bạn có muốn sửa phòng "
            + jXTable_DSLoaiphong.getValueAt(row, 1).toString()
            , "Cảnh báo", JOptionPane.YES_NO_CANCEL_OPTION
            , JOptionPane.QUESTION_MESSAGE, null, option, option[1]) == 0){

        try{
            loaiphong_info.setMaLoaiPhong(jXTable_DSLoaiphong.getValueAt(row, 2).toString());

            loaiphong_control.Delete(loaiphong_info);
            LayDS_LoaiPhong();
            clear();
            them();
        }catch(Exception e){

        }
        }
    }//GEN-LAST:event_jXButton_Them1ActionPerformed

    private void jXButton_Them2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_Them2ActionPerformed
        //Kiem tra da click vao Bảng chưa
        if(jXTable_DSLoaiphong.getSelectedRow() < 0){
            JOptionPane.showMessageDialog(null, "Vui lòng chọn Tên Phòng trong bảng danh sách"
                , "Thông tin", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        //Kiểm tra tên phòng có trống hay không
        if(jXTextField_Tenloaiphong.getText().equals("")
            || jXTextField_Dongia.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Không được để trống Tên loại phòng hoặc Đơn giá phòng"
                , "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }

        String tenphong = jXTable_DSLoaiphong.getValueAt(row, 1).toString().trim();
        if(trungTenPhong(jXTextField_Tenloaiphong.getText()) &&
            !jXTextField_Tenloaiphong.getText().equals(tenphong)){

            JOptionPane.showMessageDialog(null, "Trùng với tên phòng khác, Vui lòng chọn tên khác"
                , "Thông tin", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        //        Hỏi bạn có muốn sửa hay không
        Object option[] = {"Có", "Không"};
        if(JOptionPane.showOptionDialog(null, " Bạn có muốn sửa phòng "
            + jXTable_DSLoaiphong.getValueAt(row, 1).toString()
            , "Cảnh báo", JOptionPane.YES_NO_CANCEL_OPTION
            , JOptionPane.QUESTION_MESSAGE, null, option, option[1]) == 0){

        try{
            float a = Float.parseFloat(jXTextField_Dongia.getText().trim());
            loaiphong_info.setTenLoaiPhong(jXTextField_Tenloaiphong.getText().trim());
            loaiphong_info.setMaLoaiPhong(jXTable_DSLoaiphong.getValueAt(row, 2).toString().trim());
            loaiphong_info.setDonGiaPhong((int)a);
            System.out.println(loaiphong_info.getDonGiaPhong());
            loaiphong_control.Update(loaiphong_info);

            LayDS_LoaiPhong();
            clear();
            them();

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Lỗi sửa Loại phòng"
                , "Thông tin", JOptionPane.INFORMATION_MESSAGE);
        }
        }
    }//GEN-LAST:event_jXButton_Them2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXButton jXButton_Them;
    private org.jdesktop.swingx.JXButton jXButton_Them1;
    private org.jdesktop.swingx.JXButton jXButton_Them2;
    private org.jdesktop.swingx.JXButton jXButton_Thoat;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXLabel jXLabel4;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private org.jdesktop.swingx.JXTable jXTable_DSLoaiphong;
    private org.jdesktop.swingx.JXTextField jXTextField_Dongia;
    private org.jdesktop.swingx.JXTextField jXTextField_Maloaiphong;
    private org.jdesktop.swingx.JXTextField jXTextField_Tenloaiphong;
    // End of variables declaration//GEN-END:variables
}
